// pages/shopping/index.js
const app =getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cart: [],
        AllChecked: false,
        totalPrice: 0,
        totalNum: 0,
        chang: false,
        bluetittle: "编辑",
        redtittle: "结算",
        login:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
       
    },
    cal(e) {
        let cart = wx.getStorageSync('cart')
        var information = e.currentTarget.dataset.information
        let name = e.currentTarget.dataset.name
        let index
        if (information) { index = cart.findIndex(v => v.fid === information.fid) }
        if (name == "del") {
            if (--cart[index].num < 1) {
                cart[index].num = 1
                wx.showToast({
                    title: '不能再少啦',
                    icon: 'none',
                    duration: 1500,
                    mask: true,
                });
            }
        } else if (name == "add") {
            if (++cart[index].num >= 999) {
                cart[index].num = 999
                wx.showToast({
                    title: '不能再多啦',
                    icon: 'none',
                    duration: 1500,
                    mask: true,
                });
            }
        } else if (name == "chang") {
            cart[index].checked = !cart[index].checked;
        } else if (name == "allchang") {
            let AllChecked = !this.data.AllChecked
            cart.forEach(v => { v.checked = AllChecked })
        }
        this.setCart(cart)
    },
    set() {
        let chang = !this.data.chang
        this.setData({
            chang,
            bluetittle: chang ? "完成" : "编辑",
            redtittle: chang ? "删除" : "结算"
        })
    },
    Regler() {
        var that = this
        if (this.data.chang) {
            wx.showModal({
                title: '提示',
                content: '是否确定删除？',
                showCancel: true,
                cancelText: '取消', 
                cancelColor: '#ff2323',
                confirmText: '确定',
                confirmColor: '#000',
                success: (res) => {
                    if (res.confirm) {
                        app.DelShoppingCar()
                        that.setCart(wx.getStorageSync("cart"))
                    }
                },
                fail: () => {},
                complete: () => {}
            });
        } else {
            let cart = wx.getStorageSync('cart')||[]
            if(cart.length>0){
            wx.navigateTo({
              url: '/pages/pay/index',
            })
        }else{
            wx.showToast({
              title: '购物车还没有商品哦，快去看看心意的东西吧！',
              icon:'none'
            })
        }
        }
    },
    into(e) {
        this.setData({
            num: e.detail.value - 0
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    setCart(cart) {
        let AllChecked = true
        let totalPrice = 0
        let totalNum = 0
        AllChecked = cart.length ? true : false
        cart.forEach(v => {
            if (v.checked) {
                totalPrice +=v.num * v.discount
                totalNum += v.num
            } else {
                AllChecked = false
            }
        })
        totalPrice = totalPrice.toFixed(2)
        if (cart) {
            this.setData({
                cart,
                AllChecked,
                totalPrice,
                totalNum
            })
        }
        wx.setStorageSync("cart", cart);
    },
    onShow: function() {
        let cart = wx.getStorageSync("cart")||[];
        this.setCart(cart)
        this.setData({login:app.globalData.login})
    },
    turnto(e) {
        var information = e.currentTarget.dataset.information
        var ifm = JSON.stringify(information)
        wx.navigateTo({
            url: information.url + '?products=' + ifm,
        })
    },
})